<?php

namespace app\admin\controller;

class Adindex extends base
{

    //前置方法
    protected $beforeActionList = [
        'map' ,
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
        }
        $this->datas['lang'] = cookie('think_var_admin');
    }


    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
        $data = $this->request->post();
        $data['lang'] = cookie('think_var_admin');

        if (!empty($data['id'])) {
            return $this->update($data);
        }

        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            return ajax_info('保存成功', 'y');
        } else {
            return ajax_info('添加失败');
        }
    }

    /**
     *金龟领
     */
    public function indexjinguiling()
    {
        $list = $this->model->where($this->datas)->column('url,title,image,center', 'position');
        $this->assign('list', $list);
        return $this->fetch('indexdl');
    }

    /**
     * 设置
     */
    public function editjinguiling()
    {
        $post = $this->request->param();

        $a = model('nav')->catetree();

        $this->assign('allnav', $a);

        $where['lang'] = cookie('think_var_admin');
        $where['position'] = $post['position'];

        if ($this->request->isPost()) {
            $post['lang'] = $where['lang'];

            $this->model->where($where)->delete();

            $res = $this->model->allowField(true)->save($post);
            if ($res) {
                return ajax_info('保存成功', 'u');
            } else {
                return ajax_info('添加失败');
            }
        }

        $data = $this->model->where($where)->find();
        $this->assign('data', $data);
        return $this->fetch('editdl');
    }



    /**
     * 军区
     */
    public function indexjunqu()
    {        
        $list = $this->model->where($this->datas)->column('url,title,image', 'position');
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 设置
     */
    public function editjunqu()
    {
        $post = $this->request->param();

        $where['lang'] = cookie('think_var_admin');
        $where['position'] = $post['position'];
        if ($this->request->isPost()) {
            $post['lang'] = $where['lang'];

            $this->model->where($where)->delete();

            $res = $this->model->allowField(true)->save($post);
            if ($res) {
                return ajax_info('保存成功', 'u');
            } else {
                return ajax_info('添加失败');
            }
        }

        $data = $this->model->where($where)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }
}
